<?php
namespace app\atxunda\model;
use think\Model;
use think\Input;
use think\Cookie;
use think\Cache;
use think\Db;

class SubQuestion extends Model
{

	public  function dispose($data,$admin_id){
		if(!$data['answer']) $data['answer'] = [0];
		$data['choices'] = $data['choices'];
		$arr = array(
			'metas' => json_encode(['choices'=>$data['choices']]),
			'answer' => json_encode($data['answer']),
			'stem' => $data['stem'],
			'seek' => $data['seek'],
			'nav_id' => $data['nav_id'],
			'type_id' => $data['type_id'],
			's_id' => $data['s_id'],
			'course_id' => $data['course_id'],
		);
		$arr['created_admin_id'] = $arr['updated_admin_id'] = $admin_id;
		$arr['updated_time'] = $arr['created_time'] = time();
		return $arr;
	}

	public function editDispose($id){
		$data = self::where('id',$id)->find();
		if(!$data) return [];
		$data['choices'] = self::choicesDispose($data['metas']);
		$data['answer'] = json_decode($data['answer'],true);
		return $data;
	}

	public function getAll($condition){
		$condition = array_filter($condition);
		$where = $condition;
		if($condition['stem']){
			$stem = $condition['stem'];
			$where['stem'] = ['like',"%$stem%"];
		}
		
		if($where['nav_id']) $where['sub_question.nav_id'] = $where['nav_id'];
		if($where['type_id']) $where['sub_question.type_id'] = $where['type_id'];
		if($where['s_id']) $where['sub_question.s_id'] = $where['s_id'];
		if($where['course_id']) $where['sub_question.course_id'] = $where['course_id'];
		unset($where['page']);
		unset($where['nav_id']);
		unset($where['type_id']);
		unset($where['s_id']);
		unset($where['course_id']);
		$pageParam['query'] = $condition;
		$data = self::field('id,stem,sub_question.updated_admin_id,sub_question.updated_time,nav_name,type_name,s_name,course_name')
				->where($where)
				->join('nav','nav.nav_id=sub_question.nav_id')
				->join('type','type.type_id=sub_question.type_id')
				->join('specific','specific.s_id=sub_question.s_id')
				->join('course','course.course_id=sub_question.course_id')
				->order('sub_question.created_time desc')
				->paginate(20,false, $pageParam);
		return $data;
	}


	public function choicesDispose($metas){
		$metas = json_decode($metas,true);
		$metaArr = array();
		foreach ($metas['choices'] as $key => $v) {
			$metaArr[$key]['zimu'] = self::getZimu($key);
			$metaArr[$key]['content'] = htmlspecialchars($v);
			$metaArr[$key]['content'] = str_replace('\"', "'", $metaArr[$key]['content']);
		}
		return $metaArr;
	}

	public function getZimu($key){
		$str = '123456789';
		return ucfirst(chr(ord('A') + intval($str{$key}) - 1));
	}


	public function changeQuestion($questionId,$userId,$status,$answer){
		$createTime = $updateTime = time();
		if(Db::table('activity_question_result')->where(['questionId'=>$questionId,'userId'=>$userId])->find()){
			Db::table('activity_question_result')->where(['questionId'=>$questionId,'userId'=>$userId])->update(['status'=>$status,'answer'=>$answer,'updateTime'=>$updateTime]);
		}else{
			Db::table('activity_question_result')->insert(['questionId'=>$questionId,'userId'=>$userId,'status'=>$status,'answer'=>$answer,'updateTime'=>$updateTime,'createTime'=>$createTime]);
		}
	}
}